local class = reload("class")
local asioC = require("asioC")
local super = reload("super.basic")

---@class asioC : super.basic
local this = class(super)

function this:ctor(name, ip, port)
    super.ctor(self)
    self._name = name
    self._ip = ip
    self._port = port
    self._cid = 0
end

function this:hand()
    return function (hand)
        self._hand = hand
    end
end

function this:launch()
    asioC.link(self._name, self._ip, self._port, self)
    return self:hand()
end

---连接消息
function this:accept()
    local hand = self._hand
    hand.accept()
end

---进制消息
function this:recv(mid, pack, cid)
    local hand = self._hand
    hand.recv(mid, pack, cid)
end

function this:send(mid, pack)
    local cid = self._cid + 1
    self._cid = cid
    asioC.send(self._name, mid, pack, cid)
    return cid
end

function this:reply(mid, pack, cid)
    asioC.send(self._name, mid, pack, cid or 0)
    return cid
end

return this
